﻿using System;
using System.ComponentModel;
using System.Windows;
using Esponce.Common;
using Esponce.QRCode;

namespace Esponce.Models
{
	public class TrackCampaignViewModel : INotifyPropertyChanged
	{
		#region Constructor
		public TrackCampaignViewModel()
		{
			this.Campaign = new TrackCampaign();

#if DEBUG
			if (Settings.IsDesignMode)
			{
				this.Campaign = new TrackCampaign()
				{
					Id = "101",
					Name = "Lorem Ipsum",
					Description = "comment...",
					Created = DateTime.UtcNow,
				};
			}
#endif
		}
		#endregion

		#region Public properties
		public Actions Action
		{
			get
			{
				return this.action;
			}
			set
			{
				if (this.action != value)
				{
					this.action = value;
					OnPropertyChanged("Action");
				}
			}
		}
		private Actions action;

		public string CampaignId
		{
			get
			{
				return this.campaignId;
			}
			set
			{
				if (this.campaignId != value)
				{
					this.campaignId = value;
					OnPropertyChanged("CampaignId");
				}
			}
		}
		private string campaignId;

		public TrackCampaign Campaign
		{
			get
			{
				return this.campaign;
			}
			set
			{
				if (this.campaign != value)
				{
					this.campaign = value;
					OnPropertyChanged("Campaign");
				}
			}
		}
		private TrackCampaign campaign;
		#endregion

		#region Public methods
		public void Select()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("GET " + client.UriBuilder.BuildTrackCampaignUrl(this.CampaignId));
				this.Campaign = client.GetTrackCampaign(this.CampaignId);

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}

		public void Insert()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("PUT " + client.UriBuilder.BuildTrackCampaignUrl());
				string id = client.CreateTrackCampaign(this.Campaign);

				//Update 'id' on the form
				this.Campaign.Id = id;
				this.CampaignId = id;

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}

		public void Update()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("POST " + client.UriBuilder.BuildTrackCampaignUrl(this.CampaignId));
				client.UpdateTrackCampaign(this.Campaign);

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}

		public void Delete()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("DELETE " + client.UriBuilder.BuildTrackCampaignUrl(this.CampaignId));
				client.DeleteTrackCampaign(this.CampaignId);

				//Clear input fields on the form
				this.Campaign = new TrackCampaign();

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
